<?php

namespace api\modules\v1\models;

use Yii;
use api\common\models\Common;
use yii\data\Pagination;
use \yii\caching\FileDependency;
use \yii\db\Query;
class Recommend extends Common
{

    public static function tableName()
    {
        return '{{%position_data}}';
    }



    public function search($params){
        $cahce = Yii::$app->cache;
        $cacheKey = md5('recommend_category_'.implode('_',$params));
        $_data = $cahce->get($cacheKey);
        //$_data = false;
        if ($_data === false) {
            $fields = 'id,catid,posid,title,description,keywords,username,inputtime,like,comment,look,zst,listorder,add_time,url,thirdid';

            $query = ((new Query())
                ->select($fields)
                ->where(['istop'=>0,'posid'=>1])
                ->from(self::tableName())
            );

            $query->orderBy('inputtime desc');

            $count = $query->count();

            $pagination = new Pagination(['totalCount' => $count]);
            $pagination->defaultPageSize = Yii::$app->params['page_size'];

            $data = $query->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();

            //echo $query->offset($pagination->offset)
             //   ->limit($pagination->limit)->createCommand()->getRawSql();exit;
            
            $this->serializePagination($pagination);
            $ImgBaseUrl = self::getImgBaseUrl();
            $HtmlBaseUrl = self::getHtmlBaseUrl();
            foreach($data as $key=>$value){
                if($value['zst'] !=''){
                    $zst = [];
                    $zst_tmp = json_decode($value['zst'],1);
                    foreach($zst_tmp as $v){
                        $zst[] = $v['url'];
                    }
                    $data[$key]['zst'] = $zst;
                }else{
                    $data[$key]['zst'] = [];
                }

                //静态html地址
                if ($value['url'] != '' && substr($value['url'], -5) == '.html') {
                    $app_url = parse_url($value['url']);
                    $app_url = $HtmlBaseUrl . $app_url['path'];
                    $app_url = str_replace('/html/', '/app/', $app_url);
                    $data[$key]['url'] = $app_url;
                }
            }
            $_data = [];
            $_data['items'] = $data;
            $_data['_links'] = $this->linksEnvelope;
            $_data['_meta'] = $this->metaEnvelope;
            $cahce->set($cacheKey, $_data,Yii::$app->params['cache_recommend'],new FileDependency(['fileName' => 'cache.txt']));
        }
        return $_data;
    }


    /*
     * 置顶文章
     */
    public function getIstop(){
        $fields = 'id,catid,posid,title,description,keywords,username,inputtime,like,comment,look,zst,listorder,add_time,thirdid';
        $not_is_menu = Category::find()->select('catid')->where(['ismenu'=>0])->asArray()->all();
        $not_is_menu = implode(',',array_column($not_is_menu,'catid'));
        $data = self::find()->select($fields)->where('istop = 1 and catid not in ('.$not_is_menu.')')->orderBy('add_time desc')->asArray()->one();
        $zst = [];
        if($data){
            if($data['zst'] !=''){
                $zst_tmp = json_decode($data['zst'],1);
                foreach($zst_tmp as $v){
                    $zst[] = $v['url'];
                }
            }
            $data['zst'] = $zst;
            return $data;
        }
        return count($zst)==0 ? (object)$zst : $zst;
    }

}
